<template>
    <!-- 组件的模板 -->
    <div class="page">
        <div class="weui-cells">
            <template v-if="admins.edit_info">
                <a class="weui-cell weui-cell_access mod-team" href="javascript:;" @click="setting">
                    <div class="weui-cell__hd"><img class="pic" src="../../static/imgs/icon_enterprise.png" alt=""></div>
                    <div class="weui-cell__bd">
                        <p class="name">{{teamDetailObj.team_name}}</p>
                        <p class="id">ID：{{teamDetailObj.id}}</p>
                    </div>
                    <div class="weui-cell__ft"></div>
                </a> 
            </template>
            <template v-else>
                <a class="weui-cell weui-cell_access mod-team" href="javascript:;">
                    <div class="weui-cell__hd"><img class="pic" src="../../static/imgs/icon_enterprise.png" alt=""></div>
                    <div class="weui-cell__bd">
                        <p class="name">{{teamDetailObj.team_name}}</p>
                        <p class="id">ID：{{teamDetailObj.id}}</p>
                    </div>
                </a> 
            </template>           
        </div>
        <div class="weui-cells">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="gotoGroups">
                <div class="weui-cell__bd">
                    <p>组织架构</p>
                </div>
                <div class="weui-cell__ft">
                </div>
            </a>            
        </div>
        <div class="weui-cells" v-if="admins.transfer_role">
            <a class="weui-cell weui-cell_access" href="javascript:;" @click="transferAdminer"> 
                <div class="weui-cell__bd">
                    <p>转让超级管理员</p>
                </div>
                <div class="weui-cell__ft">
                </div>
            </a>
        </div>
    </div> 
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    import clientUi from 'gapi/clientUi.js'
    import widget from 'gapi/widget.js'
    import * as util from 'js/util.js'
    import teamApi from 'clientApi/teamApi.js'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import KCApiApp from 'clientApi/KCApiApp.js'
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */

    let teamid = ''
    let dmUserId = ''
    let backWhereParam = ''  // 用来标识从哪里来返回哪里去
    let pushDataId = ''
    let user_id = ''
    /**
     *  组件实例对象
     */
    export default {
        components: {

        }, 
        data: function(){
            return{
               admins:{
                    edit_info:false,   // 判断是否有权编辑企业信息
                    transfer_role:false  // 是否可以转让超级管理员
                },
                groupName:'',
                teamDetailObj: '',


                rootArr:[],
                super_id:'',
                currUserIdentity:'' 
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        computed: {
            params (){
                let str = 'teamId='+teamid;
                return encodeURIComponent(str)
            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init (){
                this.getPerssion()
                this.initAjax()

                window.loadData = (data) => {
                    console.log(data)
                    if(data && data.key == 'success'){ 
                        this.admins.edit_info = false
                        this.admins.transfer_role = false
                        this.getPerssion()
                    }else if(data && data.key == 'user_id'){
                        user_id = data.value
                        backWhereParam = 222
                        if(data.isAdmins == 0){
                            this.admins.edit_info = false
                            this.admins.transfer_role = false
                        }
                        this.initAjax()
                    }else{
                        this.initAjax()
                    }
                    
                }

            },
            /*获取企业信息*/
            initAjax(){
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/team/info',
                    type:'GET',
                    data:{
                        team_id:teamid,
                        project_id:0
                    },
                    timeout:10000,
                    success:(obj) => {
                        console.log(obj)
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj&&obj.errno == 0){
                            this.teamDetailObj = obj.data
                            document.title = obj.data.team_name
                            this.groupName = obj.data.team_name
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        // 无网络的回调
                        commonPage.loadPageStatus('loadSuccess') 
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            /*前往企业编辑*/
            setting (){
                //埋点
                let statisticsLog_data = {
                    event_id: '企业-企业',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'company',
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                let encodeStr = encodeURIComponent(`teamId=${teamid}`)
                location.href = `dmsaas://TeamInfo?urdData=${encodeStr}`
            },
            /*转让超级管理员*/
            transferAdminer (){
                //埋点
                let statisticsLog_data = {
                    event_id: '企业-转让超级管理员',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'change',
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                let encodeStr = encodeURIComponent(`teamId=${teamid}&user_id=${user_id}`)
                location.href = `dmsaas://TeamSelectSinglePeople?urdData=${encodeStr}`
            },
            /*前往组织架构*/
            gotoGroups (){
                //埋点
                let statisticsLog_data = {
                    event_id: '企业-组织构架',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'organization',
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                let encodeStr = encodeURIComponent(`teamId=${teamid}&dmUserId=${dmUserId}&groupName=${this.groupName}&user_id=${user_id}`)
                location.href = `dmsaas://TeamGroupList?urdData=${encodeStr}`
            },
              /*权限获取*/
            getPerssion(){
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url:'/permission/application',
                    type:'GET',
                    data:{
                        team_id:teamid,
                        project_id:0,
                        application_id:0
                    },
                    timeout:10000,
                    success:(obj) => {
                        console.log(obj)
                        commonPage.loadPageStatus('loadSuccess')
                        if(obj&&obj.errno == 0){
                            obj.data.forEach( (item) => {
                                //判断是否有权可以转让超级管理员
                                if(item.id_name == 'root_transfer_role'){
                                    this.admins.transfer_role = true;
                                    console.log(item.id_name)
                                }
                                // 判断是否有权编辑企业信息
                                if(item.id_name == 'admin_edit_team'){
                                    this.admins.edit_info = true
                                }
                            })
                            
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            }
        },
        created(){ 
            teamid = util.getUrddataString('teamId') 
            document.title = util.getUrddataString('team_name')
            dmUserId = util.getUrddataString('dmUserId')
            backWhereParam = util.getUrddataString('backWhereParam')
            user_id = util.getUrddataString('user_id')
 
            this.init();
            commonPage.getWebViewID((data)=>{
                cache.setMemoryCache('detailWebViewID',{'webViewID':data})
            })
            // 获取native标题
            commonPage.updateTitleBar({
                backAction: 'goBack',    // 点击返回按钮的动作（即回调函数名称）
                leftAction: 'goBack',    // 点击左按钮的动作（即回调函数名称）
            })

            window.goBack = () => {
                if(backWhereParam == 222){
                    cache.getMemoryCache('myIndexwebViewID',function(data){
                        // alert(JSON.stringify(data))
                        if(data){
                            commonPage.goBack(1, {'key':'kong'}, data.webViewID)
                        }
                    })
                }else if(backWhereParam == 333){
                    cache.getMemoryCache('personlibraryWebViewId',function(data){
                        if(data){
                            commonPage.goBack(1, {}, data.webViewID)
                        }
                    })
                }else if(backWhereParam == 111){
                    cache.getMemoryCache('jcIndexWebViewID',function(data){
                        if(data){
                            commonPage.goBack(1, {}, data.webViewID)
                        }
                    })
                }else{
                    cache.getMemoryCache('myIndexwebViewID',function(data){
                        if(data){
                            commonPage.goBack(1, {'key':'kong'}, data.webViewID)
                        }
                    })
                }
                
            }
        }
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/team.css"></style>



